package com.itheima.d5_synchronized_lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account3 {
    private String accountId;
    private double money;


    public Account3() {
    }

    public Account3(String accountId, double money) {
        this.accountId = accountId;
        this.money = money;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    private final Lock lock = new ReentrantLock();

    public void drawMoney(double money) {
        Thread thread = Thread.currentThread();
        String name = thread.getName();

        lock.lock();
        try {
            if (this.money >= money) {
                System.out.println(name + "取钱成功");
                System.out.println(1 / 0);
            } else {
                System.out.println(name + "取钱失败");
            }
        } finally {
            lock.unlock();
        }
    }

    @Override
    public String toString() {
        return "Account{" +
                "accountId='" + accountId + '\'' +
                ", money=" + money +
                '}';
    }

}

